package net.esj.basic.pojo.system;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import net.esj.basic.core.view.RequestUtils;
import net.esj.basic.utils.StringUtils;

public class ApplicationClass {

	public static enum APPLICATION_CAN{
		ADD,EDIT,LIST,VIEW,SORT;
	}
	
	private final Class<?> fieldClass;
	private final Map<String,Map<String,FieldAttribute>> attributes =new HashMap<String, Map<String,ApplicationClass.FieldAttribute>>();
	
	public ApplicationClass(Class<?> fieldClass) {
		super();
		this.fieldClass = fieldClass;
	}

	public Class<?> getFieldClass() {
		return fieldClass;
	}

	public FieldAttribute getAttribute(String field,String url,Map params) {
		for(String str : attributes.keySet()){
			if(StringUtils.hasText(str)){
				Map<String,String[]> map = analyzeParams(str);
				if(checkParams(map, params)){
					return attributes.get(str).get(field);
				}
			}
		}
		return attributes.get(null).get(field);
	}

	public void addAttribute(String field,String url,boolean add,boolean edit,boolean list,boolean view,boolean sort,String defaults){
		if(attributes.containsKey(url)){
			Map<String,FieldAttribute> attr = attributes.get(url);
			attr.put(field, new FieldAttribute(field,url, add, edit, list, view, sort, defaults));
		}else{
			Map<String,FieldAttribute> attr = new HashMap<String,FieldAttribute>();
			attr.put(field, new FieldAttribute(field,url, add, edit, list, view, sort, defaults));
			attributes.put(url, attr);
		}
	}
	
	private static final Map<String, String[]> analyzeParams(String url){
		if(url.indexOf("?")>0){
			String parStr = url.substring(url.indexOf("?")+1);
			Map<String, String[]> map = RequestUtils.parseQueryString(parStr);
			return map;
		}else{
			return Collections.EMPTY_MAP;
		}
	}
	
	private final boolean checkParams(Map<String, String[]> params,Map requestParams){
		boolean pass = true;
		for(Object o :params.entrySet()){
			Map.Entry<String, String[]> entry = (Entry) o;
			Object v = requestParams.get(entry.getKey());
			if(v!=null && v.getClass().isArray()){
				v = ((Object[])v)[0];
			}
			if(v== null){
				return false;
			}
			boolean has = false;
			for(String str : entry.getValue()){
				if(v.equals(str)){
					has |= true;
				}
			}
			pass &= has;
		}
		return pass;
	}
	
	public static class FieldAttribute{
		private String key;
		private String url;
		private boolean add;
		private boolean edit;
		private boolean list;
		private boolean view;
		private boolean sort;
		private String defaults;
		
		public FieldAttribute(String key,String url, boolean add, boolean edit, boolean list,
				boolean view, boolean sort, String defaults) {
			super();
			this.key = key;
			this.url = url;
			this.add = add;
			this.edit = edit;
			this.list = list;
			this.view = view;
			this.sort = sort;
			this.defaults = defaults;
		}
		public boolean isAdd() {
			return add;
		}
		public boolean isEdit() {
			return edit;
		}
		public boolean isList() {
			return list;
		}
		public boolean isView() {
			return view;
		}
		public boolean isSort() {
			return sort;
		}
		public String getKey() {
			return key;
		}
		public String getDefaults() {
			return defaults;
		}
		public String getUrl() {
			return url;
		}
		
	}
}
